//
//  DeviceViewController.m
//  MyCloud
//
//  Created by Visitor on 7/14/13.
//  Copyright (c) 2013 visitor.zc@gmail.com. All rights reserved.
//

#import "DeviceViewController.h"
#import "ImageListViewController.h"
#import "UserListViewController.h"
#import "AGImagePickerController.h"
#import "AGIPCToolbarItem.h"

@interface DeviceViewController ()

@property (nonatomic, retain) NSMutableArray *selectedPhotos;


@end

@implementation DeviceViewController

@synthesize selectedPhotos;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    MyBackBar *myBackBar = [[MyBackBar alloc] initWithTitle:@"与设备XXX连接" andType:1];
    myBackBar.delegate = self;
    myBackBar.frame = CGRectMake(0, 44, 320, 44);
    [self.view addSubview:myBackBar];
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03.png"]];
    imageView.frame = CGRectMake(35, 95, 237, 182);
    [self.view addSubview:imageView];
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imageBtn.frame = CGRectMake(35, self.view.frame.size.height - 120, 44, 44);
    //[imageBtn setTitle:@"照片" forState:UIControlStateNormal];
    [imageBtn setImage:[UIImage imageNamed:@"07.png"] forState:UIControlStateNormal];
    [imageBtn setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateSelected];
    [imageBtn setImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateHighlighted];

    [imageBtn addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
    imageBtn.tag = 1;
    [self.view addSubview:imageBtn];
    
    
    UIButton *userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userBtn.frame = CGRectMake(320-35-44, self.view.frame.size.height - 120, 44, 44);
    [userBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [userBtn setImage:[UIImage imageNamed:@"tongxunlu1.png"] forState:UIControlStateSelected];
    [userBtn setImage:[UIImage imageNamed:@"tongxunlu1.png"] forState:UIControlStateHighlighted];
    [userBtn setImage:[UIImage imageNamed:@"09.png"] forState:UIControlStateNormal];
    //[imageBtn setTitle:@"通讯录" forState:UIControlStateNormal];

    userBtn.tag = 2;
    [self.view addSubview:userBtn];
    
    userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userBtn.frame = CGRectMake(65, self.view.frame.size.height - 190, 44, 44);
    [userBtn setImage:[UIImage imageNamed:@"26.png"] forState:UIControlStateNormal];
    [userBtn setImage:[UIImage imageNamed:@"yingping.png"] forState:UIControlStateSelected];
    [userBtn setImage:[UIImage imageNamed:@"yingping.png"] forState:UIControlStateHighlighted];
    userBtn.tag = 3;
    [self.view addSubview:userBtn];

    userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userBtn.frame = CGRectMake(140, self.view.frame.size.height - 225, 44, 44);
    [userBtn setImage:[UIImage imageNamed:@"24.png"] forState:UIControlStateNormal];
    [userBtn setImage:[UIImage imageNamed:@"25.png"] forState:UIControlStateSelected];
    [userBtn setImage:[UIImage imageNamed:@"25.png"] forState:UIControlStateHighlighted];
    userBtn.tag = 4;
    [self.view addSubview:userBtn];
    
    userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    userBtn.frame = CGRectMake(215, self.view.frame.size.height - 190, 44, 44);
    [userBtn setImage:[UIImage imageNamed:@"26.png"] forState:UIControlStateNormal];
    [userBtn setImage:[UIImage imageNamed:@"yingping.png"] forState:UIControlStateSelected];
    [userBtn setImage:[UIImage imageNamed:@"yingping.png"] forState:UIControlStateHighlighted];
    userBtn.tag = 5;
    [self.view addSubview:userBtn];
    
    UIImageView *phoneImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"04.png"]];
    phoneImage.frame = CGRectMake(115, self.view.frame.size.height - 160, 88, 172);
    [self.view addSubview:phoneImage];

}

- (void)btnClick:(UIButton *)btn
{
    if(btn.tag == 1)
    {
        NSLog(@"照片按钮被点击");
        ImageListViewController *ilvc = [[ImageListViewController alloc] init];
        [self.navigationController pushViewController:ilvc animated:YES];
        [ilvc release];
    }
    else if (btn.tag == 2)
    {
        NSLog(@"通讯录按钮被点击");
        UserListViewController *ulvc = [[UserListViewController alloc] init];
        [self.navigationController pushViewController:ulvc animated:YES];
        [ulvc release];
    }
    
}

- (void)backImageClick 
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)openAction:(id)sender
{
    AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
        NSLog(@"Fail. Error: %@", error);
        
        if (error == nil) {
            [self.selectedPhotos removeAllObjects];
            NSLog(@"User has cancelled.");
            [self dismissModalViewControllerAnimated:YES];
        } else {
            
            // We need to wait for the view controller to appear first.
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [self dismissModalViewControllerAnimated:YES];
            });
        }
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        
    } andSuccessBlock:^(NSArray *info) {
        [self.selectedPhotos setArray:info];
        
        NSLog(@"Info: %@", info);
        [self dismissModalViewControllerAnimated:YES];
        
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    }];
    
    // Show saved photos on top
    imagePickerController.shouldShowSavedPhotosOnTop = YES;
    imagePickerController.selection = self.selectedPhotos;
    
    // Custom toolbar items
    AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"+ Select All" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
        return YES;
    }];
    AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease] andSelectionBlock:nil];
    AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
        return !(index % 2);
    }];
    AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"- Deselect All" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
        return NO;
    }];
    imagePickerController.toolbarItemsForSelection = [NSArray arrayWithObjects:selectAll, flexible, selectOdd, flexible, deselectAll, nil];
    //    imagePickerController.toolbarItemsForSelection = [NSArray array];
    [selectOdd release];
    [flexible release];
    [selectAll release];
    [deselectAll release];
    
    //    imagePickerController.maximumNumberOfPhotos = 3;
    [self presentModalViewController:imagePickerController animated:YES];
    [imagePickerController release];
}


@end
